<?php
	
namespace Home\Controller;
use Think\Controller;
class MemberController extends CommonController {
	public function _initialize(){
		parent::_initialize();
		//获取uid
    	$uid=I('get.uid',null,'intval');
		//会员信息
		$this->meInfo($uid);
		$this->other();
	//	
	}
	//检测用户是否存在
	public function checkMember(){
		$check_uid=I('get.uid',null,'intval');
		$status=M('user')->where(array('uid'=>$check_uid))->find();
		if(!$status) $this->error('用户不存在');
	}
	//会员信息
	public function meInfo($uid){
		//实例化user
		$user=M('user');
		$meInfo=$user->field('username,gold,exp,answer,ask,adopt,face,uid')->where(array('uid'=>$uid))->find();
		if(!$meInfo) $this->error('用户不存在！');
		$meInfo['level']=$this->exp_to_level($meInfo['exp']);
		$meInfo['face']=$this->face($meInfo);
		$meInfo['adoptBfb']=$this->adoptBfb($meInfo['answer'], $meInfo['adopt']);
		$this->assign('meInfo',$meInfo);
	}
    public function index(){
    	//获取uid
    	$uid=I('get.uid',null,'intval');
		//会员信息
		//$this->meInfo($uid);
		//我的提问
		$askInfo=D('ask');
		$meAskField='content,time,answer,cname,asid,cid';
		$meAsk=$askInfo->field($meAskField)->where(array('user.uid'=>$uid))->order('time desc')->limit(5)->select();
		$askTotal=$askInfo->where(array('user.uid'=>$uid))->count();
		$this->assign('askTotal',$askTotal);
		$this->assign('meAsk',$meAsk);
		//我的回答
		$answerInfo=D('Answerinfo');
		$meAnswerField='content,answer,time,cname';
		$meAnswer=$answerInfo->field($meAnswerField)->where(array('uid'=>$uid))->limit(5)->select();
		$answerTotal=$answerInfo->where(array('uid'=>$uid))->count();
		$this->assign('answerTotal',$answerTotal);
		$this->assign('meAnswer',$meAnswer);
		
        $this->display('member');
    }
	
	public function other(){
		$uid=I('get.uid',null,'intval');
		
		if($uid==session('uid')){
			$other='我';
		}else{
			$other='TA';
		}
		$this->assign('other',$other);
	}
	/*我的问答*/
	public function my_ask(){
		$uid=I('get.uid',null,'intval');
		$solve=isset($_GET['solve'])?I('get.solve',null,'intval'):0;
		$this->assign('solve',$solve);
		$count=D('ask')->field('content,answer,time,cname')->where(array('solve'=>$solve,'uid'=>$uid))->count();
		$page=new \Think\Page($count,8);
		$this->assign('page',$page->show());
		
		$data=D('ask')->field('content,answer,time,cname,asid,cid')->where(array('solve'=>$solve,'uid'=>$uid))->order('time desc')->limit($page->firstRow.','.$page->listRows)->select();
		$this->assign('data',$data);
		
		//empty
		if($solve!=0){
			$empty="<tr height='140'><td colspan='3'>你还没有已解决的提问</td></tr>";
		}else{
			$empty="<tr height='140'><td colspan='3'>你还没有待解决的提问</td></tr>";
		}
		
		$this->assign('empty',$empty);
		$this->display();
	}
	/*我的回答*/
	public function my_answer(){
		$uid=I('get.uid',null,'intval');
		$adopt=isset($_GET['adopt'])?I('get.adopt',null,'intval'):0;
		$this->assign('adopt',$adopt);
		
		//分页
		$count=D('Answerinfo')->where(array('adopt'=>$adopt,'uid'=>$uid))->count();
		$page=new \Think\Page($count,10);
		$this->assign('page',$page->show());
		$data=D('Answerinfo')->field('answer.content as acontent,cname,answer,atime,category.cid as ccid,ask.asid as aasid')->where(array('adopt'=>$adopt,'uid'=>$uid))->limit($page->firstRow.','.$page->listRows)->select();
		$this->assign('data',$data);
		
		//empty
		if($adopt!=0){
			$empty="<tr height='140'><td colspan='3'>我还没有被采纳的回答</td></tr>";
		}else{
			$empty="<tr height='140'><td colspan='3'>你还没有回答过问题</td></tr>";
		}
		$this->assign('empty',$empty);
		$this->display();
	}
	/*我的等级*/
	public function my_level(){
		$uid=I('get.uid',null,'intval');
		$user=M('user')->where(array('uid'=>$uid))->find();//获得用户数据
		$level=$this->exp_to_level($user['exp']);
		if(C('LV'.($level+1))==null){
			$nextExp=0;
		}else{
			$nextExp=C('LV'.($level+1))-$user['exp'];
		}

		/*我的信息*/
		$myData=array(
			'level'=>$level,
			'nextExp'=>$nextExp
		);
		$this->assign('myData',$myData);
		
		/*等级规则*/
		$levelRule=array();
		for ($i=0; $i < 21; $i++) {
			$levelRule[$i]['level']=$i; 
			$levelRule[$i]['exp']=C('LV'.$i);
		}
		$this->assign('levelRule',$levelRule);
		
		/*经验获取*/
		$exp['login']=C('LOGIN_EXP');		//每天登录
		$exp['ask']=C('LV_EXP');			//提问
		$exp['answer']=C('ANSWER_EXP');		//回答
		$exp['adopt']=C('ADOPT_EXP');		//采纳与被采纳
		$this->assign('exp',$exp);
		
		$this->display('my_level');
	}
	/*我的金币*/
	public function my_gold(){
		$gold['answer_gold']=C('ANSWER_GOLD');
		$gold['adopt_gold']=C('ADOPT_GOLD');
		$this->assign('gold',$gold);
		$this->display('my_gold');
	}
	/*我的头像*/
	public function my_face(){
		$uid=I('get.uid',null,'intval');
		$user=M('user');
		if(IS_POST){
			$uid=I('post.uid',null,'intval');
			$upload = new \Think\Upload();// 实例化上传类
		    $upload->maxSize   =     3145728 ;// 设置附件上传大小
		    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		    $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
		    $upload->savePath  =     ''; // 设置附件上传（子）目录
		    // 上传文件 
		    $info   =   $upload->uploadOne($_FILES['face']);
			
		    if(!$info) {// 上传错误提示错误信息
		        $this->error($upload->getError());
		    }else{// 上传成功
		        //$this->success('上传成功！');
			    $data['face']=__ROOT__ .'/Uploads/'.$info['savepath'].$info['savename'];
		    }
			$status=$user->where(array('uid'=>$uid))->save($data);
			$this->success('上传成功！');
		}else{
			$db=$user->field('face')->where(array('uid'=>$uid))->find();
			$db['face']=$this->face($db);
			$this->assign('face',$db['face']);
			$this->display('my_face');
		}
	}
	/*上传文件*/
	public function upload(){
		$user=M('user');
		$uid=session('uid');
		
	}
	
		
}	
